using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using UnityEngine;

namespace Miya.Hellow.Editor
{


    public static class Shell
    {
        public static void CopyDirectory(string fromdir, string todir, string prefix = "*.*")
        {
			Debug.Log($"[Shell.CopyDirectory] 开始复制目录: {fromdir} -> {todir}");
            if (!Directory.Exists(todir))
            {
                Directory.CreateDirectory(todir);
            }

            foreach (string file in Directory.GetFiles(fromdir, prefix))
            {
                string dest = Path.Combine(todir, Path.GetFileName(file));
                File.Copy(file, dest, true);
				Debug.Log($"[Shell.CopyDirectory] 复制文件: {file} -> {dest}");
            }

            foreach (string folder in Directory.GetDirectories(fromdir))
            {
                string dest = Path.Combine(todir, Path.GetFileName(folder));
                CopyDirectory(folder, dest);
            }
			Debug.Log($"[Shell.CopyDirectory] 目录复制完成: {fromdir}");
        }
        public static bool CopyFilesInDirectoty(string from, string to, string prefix = "*.*")
        {
            var all = Directory.GetFiles(from, prefix, SearchOption.AllDirectories);
			Debug.Log($"[Shell.CopyFilesInDirectoty] 开始复制文件，从 {from} 到 {to}，文件数量: {all.Length}");
            foreach (var a in all)
            {
                var bundleName = Path.GetFileName(a);
                string toname = Path.Combine(to, bundleName);
                CopyFile(a, toname);
            }
			Debug.Log($"[Shell.CopyFilesInDirectoty] 文件复制完成");
            return true;
        }
        public static   bool CopyFile(string from, string to)
        {
            try
            {
                if (!File.Exists(from)) return false;

                string path = Path.GetDirectoryName(to);

                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                
				Debug.Log($"[Shell.CopyFile] 复制文件: {from} -> {to}");
                File.Copy(from, to, true);
                return true;

            }
            catch (Exception e)
            {
                Debug.LogError($"[BuildScriptPackedMode_Distribute._CopyFile]:Error:{e}");
                return false;
            }

        }


    }

    public class ShellIni
    {
        [DllImport("kernel32")]
        private static extern string GetPrivateProfileString(string sect, string key, string def, StringBuilder retval, int size, string filepath);
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string sect, string key, string value, string filepath);



        string Path;
        StringBuilder _SB = new StringBuilder(255);
        public ShellIni(string filepath)
        {
            Path = filepath;
        }

        public string Get(string key, string sect = "")
        {
            return GetPrivateProfileString(sect, key, "", _SB, 255, Path);
        }
        public ShellIni Set(string key, string value, string sect = "")
        {
            WritePrivateProfileString(sect, key, value, Path);
            return this;
        }
    }
}

